<?php
// 更详细的FFmpeg测试脚本

// 设置错误报告级别
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 设置响应头
header('Content-Type: text/plain; charset=utf-8');

echo "=== FFmpeg 测试 ===\n\n";

// 1. 检查基本环境信息
echo "1. 环境信息:\n";
echo "PHP版本: " . PHP_VERSION . "\n";
echo "操作系统: " . PHP_OS . "\n";
echo "当前工作目录: " . getcwd() . "\n";
echo "脚本路径: " . __FILE__ . "\n";
echo "文档根目录: " . $_SERVER['DOCUMENT_ROOT'] ?? 'N/A' . "\n";
echo "\n";

// 2. 检查FFmpeg文件状态
echo "2. FFmpeg文件状态检查:\n";
$ffmpegPath = '/www/wwwroot/tbbs-api.chinesemir.com/bin/ffmpeg';

echo "文件路径: $ffmpegPath\n";
echo "文件是否存在: " . (file_exists($ffmpegPath) ? '是' : '否') . "\n";

// 检查open_basedir限制
$openBasedir = ini_get('open_basedir');
echo "open_basedir设置: " . ($openBasedir ?: '无限制') . "\n";

if ($openBasedir) {
    $allowedPaths = explode(':', $openBasedir);
    $isAllowed = false;
    foreach ($allowedPaths as $path) {
        if (strpos($ffmpegPath, rtrim($path, '/')) === 0) {
            $isAllowed = true;
            break;
        }
    }
    echo "路径是否在open_basedir允许范围内: " . ($isAllowed ? '是' : '否') . "\n";
}

echo "文件是否可读: " . (is_readable($ffmpegPath) ? '是' : '否') . "\n";
echo "文件是否可执行: " . (is_executable($ffmpegPath) ? '是' : '否') . "\n";
echo "\n";

// 3. 尝试执行FFmpeg命令
echo "3. FFmpeg执行测试:\n";

// 测试命令
$commands = [
    "$ffmpegPath -version",
    "$ffmpegPath -v",
    "which ffmpeg",
    "whereis ffmpeg"
];

foreach ($commands as $index => $command) {
    echo "\n--- 测试命令 " . ($index + 1) . ": $command ---\n";
    
    // 执行命令
    $output = [];
    $returnCode = 0;
    exec("$command 2>&1", $output, $returnCode);
    
    echo "返回码: $returnCode\n";
    echo "输出:\n" . implode("\n", $output) . "\n";
}

// 4. 检查环境变量
echo "\n4. 环境变量检查:\n";
echo "PATH: " . getenv('PATH') . "\n";

echo "\n=== 测试完成 ===\n";